package com.furns.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @Author LuoJiaCheng
 * @ID 2240129126
 * @Version 1.0
 */
public abstract class BasicServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决接收到的数据中文乱码的问题
        req.setCharacterEncoding("utf-8");
        //  System.out.println("BasicServlet doPost()");
        //获取隐藏域value值
        String action = req.getParameter("action");

        //使用反射，获取当前对象的方法
        //this,当前请求的servlet
        //declaredMethod(方法名,参数的class类)
        //declaredMethod根据用户请求变化
        //invoke(当前对象,方法参数)
        try {
            Method declaredMethod = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
            declaredMethod.invoke(this, req, resp);
        } catch (Exception e) {
            throw new RuntimeException(e);

        }


    }
}
